﻿using AutoMapper;
using Order.MicroService.IRepository;
using Order.MicroService.IService;
using Order.MicroService.Models;
using Order.MicroService.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Order.MicroService.Service
{
    public class OrderService : IOrderService
    {
        private readonly IOrderRepository _orderRepository;

        private readonly IMapper _mapper;

        public OrderService(IOrderRepository orderRepository, IMapper mapper)
        {
            _orderRepository = orderRepository;
            _mapper = mapper;
        }

        public string GetOrderInfo(string name)
        {
            var nicai = _orderRepository.GetOrders();
            if (nicai != null)
            {
                return nicai.OrderName;
            }
            else
            {
                return "错误信息";
            }
            //if (name == "")
            //{
            //    return "woaini";
            //}
            //else
            //{               
            //    return "jdl";
            //    //throw new MyException("出现错误信息，没有找到数据", 2);
            //}
        }

        public string TestOrder()
        {
            Console.WriteLine("调用OrderService的方法成功");
            return "jdlwoaini";
        }


        public string GetEntity()
        {
            return _orderRepository.GetEntity();
        }

        public IEnumerable<Orders> GetOrdersInfo()
        {
            return _orderRepository.GetOrdersInfo();
        }

        public int AddOrders(AddOrdersInfoDto orders)
        {
            //原始操作
            //Orders myOrders = new Orders();
            //myOrders.OrderName = orders.OrderName;
            //myOrders.OrderNum = orders.OrderNum;
            //myOrders.OrderStatus = orders.OrderStatus;
            //myOrders.TotalAmount = orders.TotalAmount;

            //使用AutoMapper 操作
            Orders ordersInfo = _mapper.Map<Orders>(orders);
            if (ordersInfo != null)
            {
                return _orderRepository.AddOrders(ordersInfo);
            }
            else
            {
                return 0;
            }
        }
        
        public int UpdateOrders(Orders orders)
        {
            if (orders != null)
            {
                throw new MyException("出现错误信息，没有找到数据", 2);
                //return _orderRepository.Update(orders);
            }
            else
            {
                return 0;
            }
        }
    }
}
